package com.learn.threadLocal;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/26
 */
public class ThreadLocalTest {
    ThreadLocal<String> t1 = new ThreadLocal();

//    变量
    private String content;

    public String getContent() {
//        return content;
        return t1.get();
    }

    public void setContent(String content) {
//        this.content = content;
//        将变量绑定到当前线程
        t1.set(content);
    }

    public static void main(String[] args) {
        ThreadLocalTest test = new ThreadLocalTest();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                test.setContent(Thread.currentThread().getName() + "的数据");
                System.out.println("-----------------------");
                System.out.println(Thread.currentThread().getName() + "--->" + test.getContent());
            }, "线程" + i).start();

        }
    }

}
